<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Adminbanner extends CI_Controller {

	var  $temp = 'backend';
	var $error ="";
	public function __construct()
	{
		parent::__construct();
		$this->load->library('parser');
		$this->load->helper('form');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('language');
		$this->load->helper('html');
		$this->load->library('encrypt');
		$this->load->helper('url');
		$this->load->library('image_lib');
		$this->load->library('session');
		$this->load->library('zip');
		$this->load->library('unzip');
	}
	public function index($bannerid=0,$index=1){
		if(strlen($index)<1){
			$index=1;
		}
		if(!($index>0)){
			$index = 1;
		}
		if(!($bannerid>0)){
			$bannerid = 0;
		}
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model');
			
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'error' =>$this->error,
				'modules' =>$this->functions_model->getlistfunctions_type(1)			
			);
			$this->load->model('product_model');
			$datatable=$this->product_model->loadbanner($bannerid);
			$cateid = 0;
			$catename = "";
			$imgpath ="";
			$url = "";
			$position = "";
			$active = 1;
			$bannername="";
			$imgpath="";
			$isbanner="";
			foreach($datatable as $row)
			{
				$cateid = $row->cateid;
				$catename = $row->catename;
				$bannername = $row->bannername;
				$url = $row->url;
				$imgpath = $row->imgpath;
				$active = $row->active;
				$position = $row->position;
				$isbanner = $row->isbanner;
			}
			$TotalRow= $this->product_model->loadcatetotal();
			$Pagelink=$this->config->item('base_url')."index.php/adminbanner/index/".$bannerid;
			$rownum=10;
			$listcate1=array();
			$listcate2=array();
			$listcate1=$this->product_model->loadcate(0);
			if($cateid>0)
			{
				$listcate2=$this->product_model->loadcate($cateid);
			}
			else
			{
				foreach($listcate1 as $row)
				{
					if(!($cateid>0))
					{
						$cateid=$row->cateid;
						$listcate2=$this->product_model->loadcate($cateid);
					}
				}
			}
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'error' =>$this->error,
				'modules' =>$this->functions_model->getlistfunctions_type(1),
			    'cateid' => $cateid ,
				'cateidparent' => $this->product_model->loadcateparent($cateid) ,
			    'bannername' => $bannername ,
				'bannerid' => $bannerid ,
			    'url' => $url,
			    'imgpath' => $imgpath,
			    'position' => $position,
			    'active' => $active,
				'cate1' => $listcate1,
				'cate2' => $listcate2,
				'isbanner' => $isbanner,
				'bannertable' => $this->product_model->loadbannerpage($index),
				'index' => $index,
				'pagging' => $this->BuildPage($Pagelink,$TotalRow,$index,$rownum)
			);
			//$this->load->view('backend/product',$data);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
			//$this->parser->parse($this->temp.'/'.'adminproduct', $data);
		}
		else {
			redirect('admincp/index');
		}
	}
	public function addfunctions($bannerid=0){
		$data['modules'] = $this->functions_model->getlistfunctions_type(1);
		if(isset($_SESSION['userkey'])){
			
			//Kiá»ƒm tra khi click nÃºt cáº­p nháº­t thÃªm modules
			if ($this->input->post('submit')) {
				$namemodules = $this->input->post('modname',true);
				$check = $this->functions_model->checkexit($namemodules );
				//if($check == 0){
					$mypath= "./uploads/banner";
					$file = $this->do_upload($mypath,'userfile');
					if(strlen($file)>1)
					{
						$file= "uploads/banner/".$file;
					}
					else
					{
						$file=$this->input->post('text_imgpath',true);						
					}
					//$namemodules = $this->input->post('modname',true);
					$type = $this->input->post('ftype',true);
				//}
				$this->load->model('product_model');
				$cateid=$this->input->post('dropdown_cate1',true);
				if($this->input->post('dropdown_cate2',true)>0){
					$cateid=$this->input->post('dropdown_cate2',true);
				}
				$bannername=$this->input->post('text_bannername',true);
				$catename="";
				$isbanner=$this->input->post('dropdown_isbanner',true);
				$url=$this->input->post('text_url',true);
				$imgpath=$file;
				if($this->input->post('check_active',true)=="on"){
					$active=1;
				}
				else
				{
					$active=0;
				}				
				$insert = $this->product_model->updatebanner($bannerid,$bannername,$cateid,$catename,$url,$imgpath,$active,$isbanner);

				redirect('adminbanner/index/'.$bannerid);
			}		
		}
		else {
			redirect('adminbanner/index');
		}
	}
	
	public function delete($bannerid=0,$index=1)
	{
		$this->load->model('product_model');
		$insert = $this->product_model->deletebanner($bannerid);
		redirect("adminbanner/index/0/".$index);
	
	}
	
	
	public function changecate($id)
	{
		if(isset($id)){
			$this->load->model('product_model');
			$string="<optgroup label='Thể loại mức 2'>";
			$cate2 = $this->product_model->loadcate($id);
			foreach($cate2 as $row)
			{
				$string=$string."<option value='" . $row->cateid. "'>" . $row->catename . "</option>";
			}
			$string=$string."</optgroup>";
			echo $string;
			return $string;
		}
	}
	
	

	function do_upload($mypath,$filename){
		// UPload Zip
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'jpg|jpeg|gif|png';
		$config['max_size']	= '2000';
		$this->load->library('upload', $config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}
		else 
		{
			echo $this->upload->display_errors();
		}
	}
	
	function BuildPage($PageLink,$TotalRow,$CurrentPageIndex,$PageSize)
        {
			$TotalPage = 0;
			$MaxPage=10;
			$TotalPage = floor($TotalRow / $PageSize);
			if ($TotalRow % $PageSize > 0)
			{
				$TotalPage++;
			}
			if ($CurrentPageIndex > $TotalPage)
			{
				$CurrentPageIndex = $TotalPage;
			}
			if ($CurrentPageIndex < 1)
			{
				$CurrentPageIndex = 1;
			}
			$BeginRow=($CurrentPageIndex - 1) * $PageSize + 1;;
			$EndRow=($CurrentPageIndex - 1) * $PageSize + $PageSize;
			
			$arr=explode("?",$PageLink);
			$PageLink=$PageLink."/";
			if($TotalPage<2)
			{
				return "";
			}
			
			if ($TotalPage <=$MaxPage)
			{
				$BeginPage=1;
            }
			elseif ($CurrentPageIndex == 1)
			{
				$BeginPage=1;
			}
			elseif ($CurrentPageIndex == $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex + $MaxPage / 2 >= $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex - $MaxPage / 2 <= 1)
			{
				$BeginPage= 1;
			}
			else
			{
            	$BeginPage=$CurrentPageIndex - $MaxPage / 2;
			}
			$Return="<div class=\"jb_pagination\">";
			if ($TotalPage > $MaxPage)
			{
				$Return=$Return . "<a href=\"" . (string)PageLink . "1\">Đầu</a>";
				for ($i = 0; $i < $MaxPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"p_current\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
				$Return=$Return . "<a href=\"" . (string)$PageLink . (string)$TotalPage . "\">Cuối</a>";
			}
			else
			{
				for ($i = 0; $i < $TotalPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"active\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
			}

			$Return=$Return . "</div>";

			return $Return;
        }
}
